<?php
/*
 * @Author: your name
 * @Date: 2020-10-14 16:51:53
 * @LastEditTime: 2020-12-28 10:07:49
 * @LastEditors: Please set LastEditors
 * @Description: 防火墙虚拟规则  (2020-12-10)
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/FirewallVirtualRuleService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter;

use app\common\service\module_manage\netfilter\HostLearnConfig;
use app\common\service\module_manage\netfilter\NetfilterVirtualRuleHitLog;
use app\common\service\module_manage\netfilter\VirtualRule;
use think\Db;

class FirewallVirtualRuleService extends NetfilterService
{
    /**
     * @name: 虚拟规则列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-20 10:22:59
     */
    public function virtualRuleList ($filterData)
    {
        $VirtualRule = new VirtualRule();
        $return = $VirtualRule->list($filterData);

        $fid_a = array_column($return['data'], 'fid');
        $childData = $VirtualRule->childList($filterData['host_id'], $fid_a);
        $return['data'] = $this->processOut($childData);

        return $return;
    }

        /**
     * @name: 添加虚拟规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 14:37:51
     */
    public function virtualRuleAdd ($data)
    {
        $VirtualRule = new VirtualRule();
        $insertData = $this->processInput($data, $VirtualRule);

        $this->add($insertData, $data['alias'], $VirtualRule);

    }

        /**
     * @name: 虚拟规则修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 17:24:49
     */
    public function virtualRuleModify ($updateData)
    {
        $VirtualRule = new VirtualRule();
        $addData = $this->processInput($updateData, $VirtualRule);
        
        $VirtualRule->delete($updateData['host_id'], $updateData['fid']);

        $this->add($addData, $updateData['alias'], $VirtualRule);
    }

    /**
     * @name: 规则删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 17:32:13
     */
    public function virtualRuleDelete ($input)
    {
        
        Db::name('VirtualRule')->where('host_id', $input['host_id'])->whereIn('id', $input['fid'])->delete();
        Db::name('VirtualRule')->where('host_id', $input['host_id'])->whereIn('fid', $input['fid'])->delete();
    }

    /**
     * @name: 开启规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-20 13:47:37
     */
    public function virtualRuleOn($input)
    {
        Db::name('VirtualRule')->where('host_id', $input['host_id'])->whereIn('fid', $input['fid'])->update([
            'status'=> 1
        ]);
    }

    /**
     * @name: 关闭规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-20 13:47:37
     */
    public function virtualRuleOff($input)
    {
        Db::name('VirtualRule')->where('host_id', $input['host_id'])->whereIn('fid', $input['fid'])->update([
            'status'=> 0
        ]);
    }

    /**
     * @name: 虚拟规则命中日志
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-08 14:48:22
     */
    public function virtualRuleHitLog ($input)
    {
        $HostLearnConfig= new HostLearnConfig();
        
        $HostLearnConfig->clearReming($input['host_id']);

        $NetfilterVirtualRuleHitLog = new NetfilterVirtualRuleHitLog();

        $return = $NetfilterVirtualRuleHitLog->list($input);

        return $return;
    }
}